name: Documentation

on:
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  docs:
    name: Update documentation
    runs-on: ubuntu-18.04

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Fetch tags
        run: git fetch --prune --unshallow

      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: 3.6

      - name: Install Python dependencies
        run: |
          python -m pip install --upgrade pip
          pip install mkdocs mkdocs-material mike

      - uses: actions/setup-java@v2
        with:
          java-version: 11
          distribution: 'adopt'

      - name: Get current Readium version
        run: echo "READIUM_VERSION=`git describe --tag --match [0-9]* --abbrev=0`" >> $GITHUB_ENV

      - name: Run Dokka
        run: ./gradlew dokkaGfmMultiModule

      - name: Copy README.md into docs to replace Dokka index.md
        run: cp README.md index.md

      - name: Run MkDocs
        run: |
          git config --global user.name "${GITHUB_ACTOR}"
          git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
          mike deploy ${{ env.READIUM_VERSION }}
          mike set-default ${{ env.READIUM_VERSION }}
          mike deploy --rebase --push --update-aliases ${{ env.READIUM_VERSION }} latest
